<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\controller\shop;

use app\api\controller\shop\Base;
use think\Db;

/**
 * @title 【商品】
 */
class Goods extends Base {

    /**
     * @title 积分商品
     * @method post
     * @params page 1 默认第1页
     * @params page_size 10 分页数
     */
    public function points_goods() {

        $page = input('post.page', 1);
        $page_size = input('post.page_size', 10);


        $wheres = [
            'pay_points' => ['>', 0]
        ];

        $lists = model('goods')
                ->where($wheres)
                ->field('id,title,image,pay_points')
                ->limit(($page - 1) * $page_size, $page_size)
                ->order('id desc')
                ->select();


        foreach ($lists as $key => $value) {
            $lists[$key]['image'] = APP_HOST . img_resize($value['image'], 200, 0);
        }

        return ['code' => 0, 'msg' => 'success', 'data' => $lists];
    }

    /**
     * @title 搜索
     * @method post
     * @params keywords
     * @params page 1 默认第1页
     * @params page_size 10 分页数
     * @params order 
     */
    public function search() {


        $keywords = input('post.keywords', '');
        $page = input('post.page', 1);
        $page_size = input('post.page_size', 10);
        $order = input('post.order', '');



        $map['status'] = ['=', 1];
        if ($keywords) {
            $map['title'] = ['like', '%' . $keywords . '%'];
        }


        $map_order = 'id asc';
        if (!empty($order)) {
            switch ($order) {
                case 'id':
                    $map_order = 'id desc';
                    break;
                case 'price_asc':
                    $map_order = 'price asc';
                    break;
                case 'price_desc':
                    $map_order = 'price desc';
                    break;
                case 'sale_count':
                    $map_order = 'sale_count desc';
                    break;
            }
        }

        $lists = model('goods')
                ->where($map)
                ->field('id,title,image,price')
                ->limit(($page - 1) * $page_size, $page_size)
                ->order($map_order)
                ->select();


        foreach ($lists as $key => $value) {
            $lists[$key]['image'] = APP_HOST . img_resize($value['image'], 200, 200);
        }



        return ['code' => 0, 'msg' => 'success', 'data' => $lists];
    }

    /**
     * @title 查
     * @method post
     * @params goods_id
     */
    public function read() {

        $goods_id = input('post.goods_id', 0);

        if ($goods_id) {

            $one = model('goods')->field('id,image,title,price,description,content,pay_points,quantity')->where('id', $goods_id)->find();


            $one['image'] = APP_HOST . img_resize($one['image'], 200, 200);

            // vant 懒加载
            // 使用  imageList
            // 提取所有有src值
            preg_match_all('/src\=\"(.*?)\"/', $one['content'], $matches);


            $contentImages = [];
            foreach ($matches[1] as $key0 => $value0) {
                if (strpos($value0, 'http') === FALSE) {
                    $contentImages[] = APP_HOST . $value0;
                } else {
                    $contentImages[] = $value0;
                }
            }
            $one['contentImages'] = $contentImages;

            // 评价列表  
            $elists = model('goods_evaluate')->model_where()
                    ->where('a.goods_id', $goods_id)
                    ->where('a.status', 1)
                    ->limit(3)
                    ->select();
            foreach ($elists as $key3 => $value3) {
                $elists[$key3]['nickname'] = priv_name($value3['nickname']);
            }
            $one['evaluateLists'] = $elists;

            // 头图列表
            $albums = db('goods_albums')->where('goods_id', $goods_id)->column('image');
            foreach ($albums as $key => $value) {
                $albums[$key] = APP_HOST . img_resize($value, 500, 0);
            }
            $one['albums'] = $albums;

            return ['code' => 0, 'msg' => 'success', 'data' => $one];
        } else {
            return ['code' => 1, 'msg' => 'success', 'data' => 'params missing'];
        }


        //最多10个收货地址
        // $count = $this->m_user_address
    }

    /**
     * @title 加载所有商品的评价列表
     * @method post
     * @param goods_id
     */
    public function evaluate() {

        $goods_id = input('post.goods_id', 0);

        $lists = model('goods_evaluate')->model_where()->where('a.goods_id', $goods_id)->select();

        foreach ($lists as $key3 => $value3) {
            $lists[$key3]['nickname'] = priv_name($value3['nickname']);
        }

        return ['code' => 0, 'msg' => 'success', 'data' => $lists];
    }

 

    

}
